﻿using System;
using System.Collections.Specialized;
using System.IO;
using System.Web.UI;

namespace Medianamik.UI.Web.MVP.View
{
    public abstract class PageModuleView: BasePage , IModuleView
    {
        public virtual event EventHandler Initialize;
        public virtual event EventHandler<RenderEventArgs> OverriddenRender;

        public NameValueCollection QueryString
        {
            get { return Request.QueryString; }
        }

        public void Alert(string resourceSet, string resourceKey)
        {
            Alert(Resources(resourceSet, resourceKey));
        }

        public void Redirect(string url)
        {
            Response.Redirect(url);
        }

        public Stream InputStream
        {
            get { return Page.Request.InputStream; }
        }

        public virtual string BreadcrumbTrail
        {
            get { return (String)(ViewState["BreadcrumbTrail"] ?? ""); }
            set { ViewState["BreadcrumbTrail"] = value; }
        }

        public string RawUrl
        {
            get { return Request.RawUrl; }
        }

        //public virtual bool OverrideRender { get; set; }

        protected virtual void OnRender(HtmlTextWriter writer)
        {
            var handler = OverriddenRender;
            if (handler != null)
            {
                handler(this, new RenderEventArgs(writer));
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (OverriddenRender == null)
                base.Render(writer);
            else
                OnRender(writer);
        }
    }

    public class RenderEventArgs : EventArgs
    {
        public RenderEventArgs(HtmlTextWriter writer)
        {
            Writer = writer;
        }

        public HtmlTextWriter Writer
        {
            get;
            private set;
        }
    }
}